using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
namespace TTBox
{
    public class Process
    {
        // Fields
        private const int POWER_NAME = 1;

        // Methods
        public void Beep(string soundFile)
        {
            this.Play(soundFile);
        }

        [DllImport("coredll.dll", SetLastError = true)]
        private static extern bool CloseHandle(IntPtr hObject);
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int CreateProcess(string imageName, string cmdLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, int boolInheritHandles, int dwCreationFlags, IntPtr lpEnvironment, IntPtr lpszCurrentDir, byte[] si, ProcessInfo pi);
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int GetExitCodeProcess(IntPtr hProcess, ref int lpExitCode);
        public void Play(string fileName)
        {
            try
            {
                PlaySound(fileName, IntPtr.Zero, 0x20000);
            }
            catch (Exception exception)
            {
                MessageBox.Show("Can't play sound file. " + exception.ToString());
                Application.DoEvents();
            }
        }

        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int PlaySound(string szSound, IntPtr hModule, int flags);
        public int RunNavigationMap(string strPath, string strParms)
        {
            try
            {
                System.Diagnostics.Process.Start(@"\FlashDisk\PNDSoftware\Navi\Navigator.exe", strParms);
                return 1;
            }
            catch
            {
                return 0;
            }
        }

        public int setBackLightRequirement(int powerState)
        {
            int num = -1;
            try
            {
                num = SetDevicePower("BKL1:", 1, powerState);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Set BackLight");
                Application.DoEvents();
            }
            return num;
        }

        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int SetDevicePower(string pvDevice, int dwDeviceFlags, int DeviceState);
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int SetSystemPowerState(string psState, int StateFlags, int Options);
        public int SetVolumePND(IntPtr device, uint volume)
        {
            int num = -1;
            try
            {
                num = waveOutSetVolume(device, volume);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Set Volume");
                Application.DoEvents();
            }
            return num;
        }

        public int StartProcess(string strPath, string strParms)
        {
            int num = 0;
            try
            {
                ProcessInfo pi = new ProcessInfo();
                byte[] si = new byte[0x80];
                num = CreateProcess(strPath, strParms, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, si, pi);
                WaitForSingleObject(pi.hProcess, uint.MaxValue);
                int lpExitCode = 0;
                lpExitCode = GetExitCodeProcess(pi.hProcess, ref lpExitCode);
                CloseHandle(pi.hProcess);
                CloseHandle(pi.hThread);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message + "\r\n" + exception.StackTrace, "StartProcess");
                Application.DoEvents();
            }
            return num;
        }

        public int TurnOffPND(string psState, int StateFlags, int Options)
        {
            int num = -1;
            try
            {
                num = SetSystemPowerState(psState, StateFlags, Options);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Turn-Off");
                Application.DoEvents();
            }
            return num;
        }

        [DllImport("coredll.dll", SetLastError = true)]
        private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
        [DllImport("coredll.dll", SetLastError = true)]
        protected static extern int waveOutSetVolume(IntPtr device, uint volume);

        // Nested Types
        public enum PlaySoundFlags
        {
            SND_ALIAS = 0x10000,
            SND_ALIAS_ID = 0x110000,
            SND_ASYNC = 1,
            SND_FILENAME = 0x20000,
            SND_LOOP = 8,
            SND_MEMORY = 4,
            SND_NODEFAULT = 2,
            SND_NOSTOP = 0x10,
            SND_NOWAIT = 0x2000,
            SND_RESOURCE = 0x40004,
            SND_SYNC = 0
        }

        public class ProcessInfo
        {
            // Fields
            public IntPtr hProcess;
            public IntPtr hThread;
            public IntPtr ProcessID;
            public IntPtr ThreadID;
        }
    }
 

}
